import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;


/**
 * Let's the patient update their patient information
 * 
 * @author Bethany Sumner
 * @version 1.0
 */
public class UpdatePatient extends JPanel{
	
	/**
	 * Field main.
	 */
	private MainWindow main;
	/**
	 * Field database.
	 */
	private Database database;
	/**
	 * Field patient.
	 */
	private String patient;
	
	/**
	 * Field txtPharmacy.
	 */
	private JTextField txtPharmacy;
	/**
	 * Field txtInsurance.
	 */
	private JTextField txtInsurance;
	/**
	 * Field txtAllergies.
	 */
	private JTextField txtAllergies;
	/**
	 * Field lblMessage.
	 */
	private JLabel lblMessage;
	/**
	 * Field btnUpdate.
	 */
	private JButton btnUpdate;
	/**
	 * Field btnCancel.
	 */
	private JButton btnCancel;
	/**
	 * Field txtFirstName.
	 */
	private JTextField txtFirstName;
	/**
	 * Field txtLastName.
	 */
	private JTextField txtLastName;
	/**
	 * Field txtPhone.
	 */
	private JTextField txtPhone;
	/**
	 * Field txtEmail.
	 */
	private JTextField txtEmail;
	/**
	 * Field rdbtnMale.
	 */
	private JRadioButton rdbtnMale;
	/**
	 * Field rdbtnFemale.
	 */
	private JRadioButton rdbtnFemale;
	/**
	 * Field rdbtnHermapherdite.
	 */
	private JRadioButton rdbtnHermapherdite;
	/**
	 * Field comboMonth.
	 */
	private JComboBox comboMonth;
	/**
	 * Field comboDay.
	 */
	private JComboBox comboDay;
	/**
	 * Field comboYear.
	 */
	private JComboBox comboYear;
	
	/**
	 * Creates the Update Patient Panel
	 * @param main Reference to the MainWindow
	 * @param database Reference to the database
	 * @param patient The username of the patient of whom to update information
	 */
	public UpdatePatient(MainWindow main, Database database, String patient) {
		
		this.main = main;
		this.database = database;
		this.patient = patient;
		
		createLayout();
		populatefields();
		
	}
	
	/**
	 * Create the GUI layout
	 */
	private void createLayout(){
		setPreferredSize(new Dimension(800, 650));
		setLayout(null); //absolute positioning
		
		// create the message
		lblMessage = new JLabel("Update the patient information for " + patient + ":");
		lblMessage.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		lblMessage.setBounds(31, 25, 583, 29);
		add(lblMessage);
		
		// create ButtonListener
		ButtonListener buttonListener = new ButtonListener();
		
		// create "update user" button
		btnUpdate = new JButton("Update Information");
		btnUpdate.setBounds(336, 615, 171, 29);
		btnUpdate.addActionListener(buttonListener);
		add(btnUpdate);
		
		// make cancel button
		btnCancel = new JButton("Cancel");
		btnCancel.setBounds(209, 615, 115, 29);
		btnCancel.addActionListener(buttonListener);
		add(btnCancel);
		
		// first name field
		JLabel lblFirstName = new JLabel("First Name:");
		lblFirstName.setBounds(31, 88, 117, 16);
		add(lblFirstName);
		txtFirstName = new JTextField();
		txtFirstName.setBounds(160, 82, 199, 28);
		add(txtFirstName);
		txtFirstName.setColumns(10);
		
		// last name field
		JLabel lblLastName = new JLabel("Last Name:");
		lblLastName.setBounds(31, 133, 102, 16);
		add(lblLastName);
		txtLastName = new JTextField();
		txtLastName.setBounds(160, 128, 199, 28);
		add(txtLastName);
		txtLastName.setColumns(10);
		
		// phone number field
		JLabel lblPhone = new JLabel("Phone Number:");
		lblPhone.setBounds(31, 178, 110, 16);
		add(lblPhone);
		txtPhone = new JTextField();
		txtPhone.setBounds(160, 174, 199, 28);
		add(txtPhone);
		txtPhone.setColumns(10);
		
		// email field
		final JLabel lblEmail = new JLabel("E-mail Address:");
		lblEmail.setBounds(31, 223, 117, 16);
		add(lblEmail);
		txtEmail = new JTextField();
		txtEmail.setBounds(160, 220, 199, 28);
		add(txtEmail);
		txtEmail.setColumns(10);
		
		// sex field
		final JLabel lblSex = new JLabel("Sex:");
		lblSex.setBounds(31, 268, 61, 16);
		add(lblSex);
		rdbtnMale = new JRadioButton("Male");
		rdbtnMale.setBounds(160, 267, 72, 23);
		add(rdbtnMale);
		rdbtnFemale = new JRadioButton("Female");
		rdbtnFemale.setBounds(238, 267, 81, 23);
		add(rdbtnFemale);
		rdbtnHermapherdite = new JRadioButton("Hermaphrodite");
		rdbtnHermapherdite.setBounds(319, 267, 126, 23);
		add(rdbtnHermapherdite);
		final ButtonGroup sexButtonGroup = new ButtonGroup();
		sexButtonGroup.add(rdbtnHermapherdite);
		sexButtonGroup.add(rdbtnMale);
		sexButtonGroup.add(rdbtnFemale);
		rdbtnFemale.setSelected(true); // Make female the default value
		
		// date of birth field
		final JLabel lblDateOfBirth = new JLabel("Date of Birth:");
		lblDateOfBirth.setBounds(31, 308, 102, 16);
		add(lblDateOfBirth);
		comboMonth = new JComboBox();
		comboMonth.setBounds(160, 302, 110, 27);
		comboMonth.addItem("January");
		comboMonth.addItem("Febuary");
		comboMonth.addItem("March");
		comboMonth.addItem("April");
		comboMonth.addItem("May");
		comboMonth.addItem("June");
		comboMonth.addItem("July");
		comboMonth.addItem("August");
		comboMonth.addItem("September");
		comboMonth.addItem("October");
		comboMonth.addItem("November");
		comboMonth.addItem("December");
		add(comboMonth);
		JLabel slash1 = new JLabel("/");
		slash1.setBounds(282, 307, 14, 16);
		add(slash1);
		comboDay = new JComboBox();
		comboDay.setBounds(298, 302, 71, 27);
		for(int i = 1; i<=31; i++){
			if(i < 10){
				comboDay.addItem("0" + i);
			}else{
				comboDay.addItem("" + i);
			}
		}
		add(comboDay);
		JLabel slash2 = new JLabel("/");
		slash2.setBounds(381, 307, 14, 16);
		add(slash2);
		comboYear = new JComboBox();
		comboYear.setBounds(401, 302, 92, 27);
		for(int i = 0; i<=100; i++){
			comboYear.addItem("" + (2011 - i));
		}
		add(comboYear);
		
		// pharmacy field
		JLabel lblPharmacy = new JLabel("Pharmacy:");
		lblPharmacy.setBounds(31, 349, 78, 14);
		add(lblPharmacy);
		txtPharmacy = new JTextField();
		txtPharmacy.setBounds(160, 345, 199, 28);
		add(txtPharmacy);
		txtPharmacy.setColumns(10);
		
		// insurance field
		JLabel lblInsurance = new JLabel("Insurance:");
		lblInsurance.setBounds(31, 392, 78, 14);
		add(lblInsurance);
		txtInsurance = new JTextField();
		txtInsurance.setBounds(160, 388, 199, 28);
		add(txtInsurance);
		txtInsurance.setColumns(10);
		
		// allergies field
		JLabel lblAllergies = new JLabel("Allergies:");
		lblAllergies.setBounds(31, 435, 78, 14);
		add(lblAllergies);
		txtAllergies = new JTextField();
		txtAllergies.setBounds(160, 428, 252, 124);
		add(txtAllergies);
		txtAllergies.setColumns(10);
		
	} // end createLayout method
	
	/**
	 * Populates the fields based on information about the currentPatient
	 */
	private void populatefields() {
		try {
			ArrayList<String> info = database.getPatientInfo(patient);
			System.out.println(info.size());
			//[firstName, lastName, patientID, email, F, 01 January 2011, pharmacy, insurance, allergies]
			int first = 0, last = 1, email = 3, sex = 4, dob = 5, pharmacy = 6, insurance = 7, allergies = 8, phone = 9; // lists index of where to find the information in the info ArrayList
			
			txtFirstName.setText(info.get(first));
			txtLastName.setText(info.get(last));
			txtPhone.setText(info.get(phone));
			txtEmail.setText(info.get(email));
			
			//set sex
			switch(info.get(sex).charAt(0)){
			case 'M':
				rdbtnMale.setSelected(true);
				break;
			case 'F':
				rdbtnFemale.setSelected(true);
				break;
			case 'H':
				rdbtnHermapherdite.setSelected(true);
				break;
			}
			
			// set date of birth
			String[] dayMonthYear = info.get(dob).split(" ");
			comboDay.setSelectedItem(dayMonthYear[0]);
			comboMonth.setSelectedItem(dayMonthYear[1]);
			comboYear.setSelectedItem(dayMonthYear[2]);
			
			txtPharmacy.setText(info.get(pharmacy));
			txtInsurance.setText(info.get(insurance));
			txtAllergies.setText(info.get(allergies));

		} catch (SQLException e) {
			//e.printStackTrace();
			main.showMessage("Error getting information from database");
			Logs log = new Logs();
			log.logNonError(Level.FINE, "Could not retrieve information from the database pertaining to patient information");
		}
	} 
	
	/**
	 * Cancels the changes, closes the panel
	 */
	private void cancel(){
		main.removeTab(this);
	}
	
	/**
	 * Updates the patient information
	 */
	private void updateInfo() {
		String gender, dateOfBirth;
		
		if(rdbtnMale.isSelected()){
			gender = "M";
		}else if(rdbtnFemale.isSelected()){
			gender = "F";
		}else{
			gender = "H";
		}
		
		dateOfBirth = comboDay.getSelectedItem() + " " + comboMonth.getSelectedItem() + " " + comboYear.getSelectedItem();
		
		
		try {
			database.updatePatientInformation(txtFirstName.getText(), txtLastName.getText(), this.patient, txtEmail.getText(), gender, dateOfBirth, txtPharmacy.getText(),
					txtInsurance.getText(), txtAllergies.getText(), txtPhone.getText());
			main.showMessage("Your information has been successfully updated");
			main.removeTab(this);
		} catch (SQLException e) {
			main.showError("Error updating patient information");
			e.printStackTrace();
		}
	}


	/**
	 * ButtonListener
	 * ActionListener to listen when button has been pressed
	 *
	 *
	 *  Date           	Name            	Action
	 *  --------------------------------------------------------------------------------------
	 *	03/06/11		Bethany				Created Class
	 *
	 *
	 *
	 * @author bethanysumner
	 */
	private class ButtonListener implements ActionListener{
		
		/**
		 * Performs tasks corresponding to the action
		 * @param event The action event
		 * @see java.awt.event.ActionListener#actionPerformed(ActionEvent)
		 */
		public void actionPerformed(ActionEvent event){
			
			if(event.getSource().equals(btnUpdate)){
				updateInfo();
			}else if(event.getSource().equals(btnCancel)){
				cancel();
			}
			
		}
		
	}// end ButtonListener
	
}